package com.example.crm.servlet;

import com.example.crm.dao.CustomerDAO;
import com.example.crm.domain.Customer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

@WebServlet("/listcustomer")
public class AllCustomerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        CustomerDAO dao = new CustomerDAO();
        List<Customer> list = new ArrayList<>();
        try {
             list =  dao.findAll();

            System.out.println(list.toArray());
            if(list!=null)
            {
                req.setAttribute("list",list);

                ListIterator listIt = list.listIterator();
                System.out.println("--------ListIterator正向迭代----------");
                while(listIt.hasNext()){
                    System.out.println(listIt.next());
                }

            }
            else
            {
                resp.getWriter().print("find fail");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        req.getRequestDispatcher("jsp/customer/list.jsp").forward(req, resp);

    }
}
